import json

from django.db.models import Q
from django.http import JsonResponse, HttpResponse

from Friend.models import Friend
from Group.models import Group_User


def query(request):
    f = Friend.objects.filter(account=request.GET.get("account"))
    if len(f) != 0:
        return JsonResponse({"data": [{
            'account': each.account,
            'friendAccount': each.friendAccount,
            'remark': each.remark
        } for each in f.order_by('-createTime')]
        })
    else:
        return HttpResponse("没有好友")
def querySingle(request):
    f = Friend.objects.filter(account=request.GET.get("account"),friendAccount=request.GET.get("friendAccount"))
    if len(f) != 0:
        return JsonResponse(
            {"account": f[0].account,
             "friendAccount": f[0].friendAccount,
             "remark": f[0].remark#备注
             })
    else:
        return HttpResponse("没得好友")
def createFriend(request):
    if request.method == "POST":
        body = json.loads(request.body)
        f = Friend.objects.filter(account=body["account"], friendAccount=body["friendAccount"])
        if len(f) != 0:
            return HttpResponse("你们已经是好友了")
        else:
            Friend.objects.create(
                account=body["account"],
                friendAccount=body["friendAccount"],
                remark=body["remarkFriend"]
            ).save()
            Friend.objects.create(
                friendAccount=body["account"],
                account=body["friendAccount"],
                remark=body["remarkMe"]
            ).save()
            return HttpResponse("添加好友成功")
    else:
        return HttpResponse("请使用POST")
def modifyRemark(request):
    if request.method == "POST":
        body = json.loads(request.body)
        f = Friend.objects.filter(account=body["account"],friendAccount=body["friendAccount"])
        if len(f) != 0:
            f.update(remark=body["remark"])
            return HttpResponse("修改成功")
        else:
            return HttpResponse("未找到用户信息")
    else:
        return HttpResponse("请使用POST")


def dropFriends(request):
    if request.method == "POST":
        body = json.loads(request.body)
        f = Friend.objects.filter(account=body["account"],friendAccount=body["friendAccount"])
        f1 = Friend.objects.filter(friendAccount=body["account"],account=body["friendAccount"])
        if len(f) != 0:
            f.delete()
            f1.delete()
            return HttpResponse("删除好友成功")
        else:
            return HttpResponse("未找到好友信息")
    else:
        return HttpResponse("请使用POST")

def getFriendsWhoNotInGroup(request):
    f = Friend.objects.filter(account=request.GET.get("account"))
    if len(f) != 0:
        arr=[]
        for each in f:
            g=Group_User.objects.filter(account=each.friendAccount,groupId=request.GET.get("groupId"))
            if len(g)==0:
                arr.append(each)
        return JsonResponse({"data":[
            {
                "remark":each.remark,
                "account":each.account,
                "friendAccount":each.friendAccount,
                "createTime":each.createTime
            }for each in arr
        ]})
    else:
        return HttpResponse("没得好友")
